﻿using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using WindowsAPI.Service;

namespace WindowsAPI
{
    [SuppressUnmanagedCodeSecurity, Dll(FileName)]
    public static class psapi
    {
        /* ------------------------------------------------------------------------------------------ */

        public const string FileName = "psapi.dll";

        /* ------------------------------------------------------------------------------------------ */

        [DllImport(FileName, SetLastError = true)]
        public static extern bool EnumDeviceDrivers(
            [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U4)] [In][Out] uint[] ddAddresses,
            uint arraySizeBytes,
            [MarshalAs(UnmanagedType.U4)] out uint bytesNeeded
        );

        [DllImport(FileName, SetLastError = true)]
        public static extern int GetDeviceDriverBaseName(
            uint ddAddress,
            StringBuilder ddBaseName,
            int baseNameStringSizeChars
        );

        [DllImport(FileName, SetLastError = true)]
        public static extern uint GetModuleBaseName(IntPtr hProcess, IntPtr hModule, [Out] StringBuilder lpBaseName, uint nSize);

        /// <summary>Перечисление модулей процесса</summary>
        [DllImport(FileName, SetLastError = true)]
        public static extern uint GetModuleFileNameEx(
            IntPtr hProcess,
            IntPtr hModule,
            [Out] StringBuilder lpBaseName,
            [In] [MarshalAs(UnmanagedType.U4)] int nSize);

        [DllImport(FileName, SetLastError = true)]
        public static extern bool EnumProcessModules(
            IntPtr hProcess,
            [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U4)] [In][Out] uint lphModule,
            uint cb,
            [MarshalAs(UnmanagedType.U4)] out uint lpcbNeeded);

        [DllImport(FileName, SetLastError = true)]
        public static extern bool EnumProcessModules(
            IntPtr hProcess,
            [In][Out] uint[] lphModules,
            uint cb,
            [MarshalAs(UnmanagedType.U4)] out uint lpcbNeeded);

        [DllImport(FileName, SetLastError = true)]
        public static extern bool EnumProcessModules(
            IntPtr hProcess,
            [Out] IntPtr lphModules,
            uint cb,
            [MarshalAs(UnmanagedType.U4)] out uint lpcbNeeded);

        [DllImport(FileName, SetLastError = true)]
        public static extern uint GetProcessImageFileName(
            IntPtr hProcess,
            [Out] StringBuilder lpImageFileName,
            [In] [MarshalAs(UnmanagedType.U4)] int nSize);
    }
}

